package com.sx.gongzhong.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.pest.RegisterPest;
import com.sx.gongzhong.pest.VerifyCodePest;
import com.sx.gongzhong.utils.BaseUtil;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.IVoidView;
import com.sx.gongzhong.view.VerifyCodeView;
import com.sx.gongzhong.widget.LoadDialog;
import com.sx.gongzhong.widget.MyRatingBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity implements VerifyCodeView, IVoidView {

    @BindView(R.id.et_UserName)
    EditText et_UserName;
    @BindView(R.id.et_Phone)
    EditText et_Phone;
    @BindView(R.id.et_Password)
    EditText et_Password;
    @BindView(R.id.rb_RatingBar)
    MyRatingBar rb_RatingBar;
    @BindView(R.id.tv_Strong)
    TextView tv_Strong;
    @BindView(R.id.et_Message)
    EditText et_Message;
    @BindView(R.id.tv_GetCode)
    TextView tv_GetCode;

    private long time = 60*1000;
    private CountDownTimer timer;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        setTitleName(R.string.register);
        ButterKnife.bind(this);
        initViews();
    }

    private void initViews() {
        rb_RatingBar.setClickable(false);
        et_Password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 6 && s.length() < 8){
                    tv_Strong.setText("很弱");
                    rb_RatingBar.setStar(1);
                }
                if (s.length()>=8){
                    if ((s.toString()).matches("[0-9]+")||(s.toString()).matches("[a-zA-Z]+")){
                        tv_Strong.setText("一般");
                        rb_RatingBar.setStar(2);
                    }else {
                        tv_Strong.setText("安全");
                        rb_RatingBar.setStar(3);
                    }
                }
            }
        });
    }
    String loginName, password, phone, verifyCode;
    @OnClick({R.id.tv_GetCode,R.id.btn_Submit})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_GetCode:
                tv_GetCode.setClickable(false);
                if (!BaseUtil.isEmpty(et_Phone.getText().toString().trim())){
                    new VerifyCodePest(this).getVerifyCode(et_Phone.getText().toString().trim(),1);
                }else {
                    ToastUtils.showToast(RegisterActivity.this,"请先输入手机号码");
                }
                break;
            case R.id.btn_Submit:
                loginName = et_UserName.getText().toString().trim();
                if (BaseUtil.isEmpty(loginName)){
                    et_UserName.setError("请输入登录名");
                    return;
                }
                if (loginName.length() < 2 || loginName.length() > 20){
                    et_UserName.setError("请输入2-20位");
                    return;
                }
                if (checkisWord(loginName)) {
                    ToastUtils.showToast(this, "登录名不能为纯字母");
                    return;
                }
                if (isNumeric(loginName)) {
                    ToastUtils.showToast(this, "登录名不能为纯数字");
                    return;
                }
                phone = et_Phone.getText().toString().trim();
                if (BaseUtil.isEmpty(phone)){
                    et_Phone.setError("请输入手机号");
                    return;
                }
                if (phone.length() != 11){
                    et_Phone.setError("请输入正确手机号");
                    return;
                }
                password = et_Password.getText().toString().trim();
                if (BaseUtil.isEmpty(password)){
                    et_Password.setError("请输入密码");
                    return;
                }
                if (password.length() < 6 || password.length() > 20 ){
                    et_Password.setError("密码长度为6-20位");
                    return;
                }
                verifyCode = et_Message.getText().toString().trim();
                if (BaseUtil.isEmpty(verifyCode)){
                    et_Message.setError("请输入验证码");
                    return;
                }
                LoadDialog.show(RegisterActivity.this,"注册中...");
                new RegisterPest(RegisterActivity.this).register(loginName, password, phone, verifyCode);
//                new VerifyCodePest(this).checkVerifyCode(phone, verifyCode,1);
                break;
        }
    }

    public boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public boolean checkisWord(String str) {
        boolean isWord = true;
        char[] chrCharArray = str.toCharArray(); //创建一个字符数组chrCharArray //将字符串变量转换为字符数组
        for (int i = 0; i < chrCharArray.length; i++) {
            if ((chrCharArray[i] >= 'a' && chrCharArray[i] <= 'z') || (chrCharArray[i] >= 'A' && chrCharArray[i] <= 'Z')) {
            } else {
                isWord = false;
            }
        }
        return isWord;
    }

    private void startTime() {
        timer=new CountDownTimer(time,998) {
            @Override
            public void onTick(long millisUntilFinished) {
                time-=1000;
                if(time>0){
                    tv_GetCode.setText(""+time/1000+"s");
                    tv_GetCode.setTextColor(Color.parseColor("#fa4040"));
                }
            }
            @Override
            public void onFinish() {
                tv_GetCode.setText("重新获取");
                tv_GetCode.setTextColor(Color.parseColor("#000000"));
                tv_GetCode.setClickable(true);
            }
        }.start();
    }

    @Override
    protected void onDestroy() {
        if (timer!=null){
            timer.cancel();
            timer.onFinish();
            timer=null;
        }
        super.onDestroy();
    }
    //获取验证码
    @Override
    public void getVerifyCodeSuccess(Boolean result) {
        time = 60*1000;
        startTime();
    }
    //校验验证码
    @Override
    public void checkVerifyCodeSuccess(String result) {
        new RegisterPest(RegisterActivity.this).register(loginName, password, phone, verifyCode);
    }
    @Override
    public void showError(String message) {
        LoadDialog.dismiss(RegisterActivity.this);
        ToastUtils.showToast(RegisterActivity.this,message);
    }
    //注册成功
    @Override
    public void voidView(String s) {
        LoadDialog.dismiss(RegisterActivity.this);
        ToastUtils.showToast(RegisterActivity.this,"注册成功");
        Intent intent = new Intent();
        intent.putExtra("UserName",loginName);
        setResult(RESULT_OK,intent);
        finish();
    }
}
